
<?php
include "connectDB.php";
$fun = $_POST["function"];

if($fun == 'calmoney'){calmoney();}
else if($fun == 'onload'){onload();}

function calmoney(){
  $MerID = $_POST["MerID"];
  $item = $_POST["item"];
  $torn = $_POST["total"];
  $torn_total = $torn;
  do{
    if($torn >= 1000){$piece_1000 = floor($torn/1000);$torn = $torn%1000;}
    else if($torn < 1000 && $torn >=500){$piece_500 = floor($torn/500);$torn = $torn%500;}
    else if($torn < 500 && $torn >= 100){$piece_100 = floor($torn/100);$torn = $torn%100;}
    else if($torn < 100 && $torn >= 20){$piece_20 = floor($torn/20);$torn = $torn%20;}
    else if($torn < 20 && $torn >= 10){$piece_10 = floor($torn/10);$torn = $torn%10;}
    else if($torn < 10 && $torn >= 5){$piece_5 = floor($torn/5);$torn = $torn%5;}
    else if($torn <  5 && $torn >=0) {$torn=0;}
  }
  while($torn != 0);

  $arr = array('torn' => $torn_total, 'piece_1000' => $piece_1000, 'piece_500' => $piece_500, 'piece_100' => $piece_100,'piece_20' => $piece_20,'piece_10' => $piece_10,'piece_5' => $piece_5,'piece_1' => $torn);
  //$arr = array('torn' => $torn_total);
  echo json_encode($arr);
}
function onload(){ // gen ddl
  $MerID = $_POST["MerID"];
  $strSQL = "SELECT * FROM tbl_item WHERE mer_id = '".$MerID."' ";
  $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
  while($rs=mysql_fetch_array($result)) {
                $variable1 = $rs["item_name"];
                $variable2 = $rs["price"];
                echo "<OPTION VALUE=\"$variable2\">".$variable1.'</option>';
  }
}
?>
